﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;

namespace SPGhostFilesManager
{
    public partial class SPGhostListFileDialog : Form
    {
        public SPGhostListFileDialog()
        {
            InitializeComponent();
        }

        private SPListFileItem _itemToGhost;

        public DialogResult ShowDialog(IWin32Window owner, SPList list, SPListFileItem itemToGhost)
        {
            _itemToGhost = itemToGhost;
            _fileToGhost.Text = _itemToGhost.Url;
            BindFilesGridView(list);
            return ShowDialog(owner);
        }

        private void BindFilesGridView(SPList list)
        {
            _filesGridView.DataSource = null;
            List<SPListFileItem> items = new List<SPListFileItem>();
            foreach (SPListItem item in list.Items)
            {
                if (item.File != null && item.File.CustomizedPageStatus == SPCustomizedPageStatus.Uncustomized)
                    items.Add(new SPListFileItem(item));
            }
            _filesGridView.DataSource = new SortableBindingList<SPListFileItem>(items);
        }

        private void _ghost_Click(object sender, EventArgs e)
        {
            SPListFileItem item = _filesGridView.SelectedRows[0].DataBoundItem as SPListFileItem;
            _itemToGhost.Ghost(item.Url);
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
